﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DCT.Huiteng.AdminSystem.Web.Codes;
using DCT.Huiteng.Utility;
using System.Net;
using System.IO;

namespace DCT.Huiteng.AdminSystem.Web.toolpages
{
    public partial class SSOToolPage : ToolBasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnCreateRandomKey_Click(object sender, EventArgs e)
        {
            this.txtRandomKey.Text = StringUtility.CreateRnd(10, true, true, false, false, "");
        }

        protected void btnGenerateSSOKey_Click(object sender, EventArgs e)
        {
            string result = "";
            string user = this.txtGenerateUser.Text;
            string source = this.txtGenerateSource.Text;
            string randomKey = this.txtGenerateRandom.Text;
            string tick = DateTimeUtility.DateTimeFrom19700101(DateTime.Now.AddMinutes(-4)).ToString();

            string url = "~/api.ashx?func=CreateSSOToken&source={0}&user={1}&tick={2}&key={3}";
            string raw = source + user + tick + randomKey;
            string md5raw = StringUtility.MD5(raw);
            url = string.Format(url
                , source
                , user
                , tick
                , md5raw);

            url = HttpExtUtility.GetFullUrlPrefix() + this.ResolveUrl(url);

            result += "raw=" + raw + Environment.NewLine;
            result += "md5raw=" + md5raw + Environment.NewLine;
            result += url + Environment.NewLine;

            var r = WebRequest.Create(url);
            r.Method = "GET";
            WebResponse response = null;
            try
            {
                response = r.GetResponse();
                using (var reader = new StreamReader(response.GetResponseStream()))
                {
                    result += reader.ReadToEnd();
                }
            }
            catch (Exception ex)
            {
                result += ex.Message;
                return;
            }
            finally
            {
                if (response != null)
                {
                    response.Close();
                }
            }

            //result为Json字符串
            this.txtSSOKeyResult.Text = result;
        }

        protected void btnValidSSOKey_Click(object sender, EventArgs e)
        {
            string n = Environment.NewLine;
            string token = this.txtValidSSOKey.Text;
            Dictionary<string , string> cachedata = null;
            bool valid = SSOTokenValidator.Check(token, out cachedata);


            string msg = "valid=" + valid.ToString() + n;

            if (valid) {
                msg += "cachedata====>" + n;
                foreach (var item in cachedata)
                {
                    msg += (item.Key + "=" + item.Value + n);
                }
            }

            this.txtValidSSOKeyResult.Text = msg;
        }
    }
}